package com.gz.furnitures.service.impl;

import com.gz.furnitures.dao.impl.FurnDaoImpl;
import com.gz.furnitures.dao.impl.OrderDaoImpl;
import com.gz.furnitures.dao.impl.OrderItemDaoImpl;
import com.gz.furnitures.entity.*;
import com.gz.furnitures.service.OrderService;
import jdk.nashorn.internal.ir.CallNode;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class OrderServiceImpl implements OrderService {
    OrderDaoImpl orderDao = new OrderDaoImpl();
    OrderItemDaoImpl orderItemDao = new OrderItemDaoImpl();
    FurnDaoImpl furnDao = new FurnDaoImpl();



    @Override
    //返回订单号
    public String saveOrder(Cart cart, int memberId) {
        //将购物车cart中的信息保存到order和orderItem形式保存到db中
        //1.通过cart对象，构建出对应的Order对象
        //先生成一个UUID表示当前的订单号，订单号要保证唯一
        String orderId = System.currentTimeMillis()+""+memberId;
        Order order = new Order(orderId, LocalDateTime.now(),cart.getPageTotalPrice(),0,memberId);
        //保存到order数据表
        orderDao.saveOrder(order);

        //2.通过cart对象，遍历出CartItem,构建OrderItem对象，并保存在order_item表中
        LinkedHashMap<Integer, CartItem> items = cart.getItems();
        Set<Integer> keySet = items.keySet();
        for (Integer id : keySet) {
            CartItem item = items.get(id);
            OrderItem orderItem = new OrderItem(null,item.getName(),item.getPrice(),
                    item.getCount(),item.getTotalPrice(),orderId);
            orderItemDao.saveOrderItem(orderItem);

            //3.更新furn表的销量和存量
            //(1)根据id获得furn对象
            Furn furn = furnDao.queryFurnById(id);
            furn.setSales(furn.getSales()+item.getCount());
            furn.setStock(furn.getStock()-item.getCount());
            furnDao.updateFurn(furn);
        }
        //清空购物车
        cart.clear();
        return orderId;
    }

    @Override
    public List<Order> queryOrder() {
        return orderDao.queryOrder();
    }

    //根据会员id获取订单列表
    @Override
    public List<Order> getOrdersByMemberId(int begin, int pageSize, int memberId) {
        return orderDao.getOrdersByMemberId(begin,pageSize,memberId);
    }

    //根据订单号获取家具列表
    @Override
    public List<OrderItem> getPageOrderItemsByOrderId(int begin, int pageSize, String orderId) {
        return orderItemDao.getPageOrderItemsByOrderId(begin,pageSize,orderId);
    }

    @Override
    public Page<Order> orderPage(int pageNo, int pageSize, int memberId) {
        Page<Order> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        int totalRow=orderDao.getTotalRowByMemberId(memberId);
        page.setTotalRow(totalRow);
        int pageTotalCount=totalRow/pageSize;
        if(totalRow%pageSize>0){
            pageTotalCount++;
        }
        page.setPageTotalCount(pageTotalCount);
        //这里的begin需要计算
        //注意这里有一个坑
        int begin=(pageNo-1)*pageSize;
        List<Order> pageOrdersByMemberId = getOrdersByMemberId(begin, pageSize, memberId);
        page.setItems(pageOrdersByMemberId);
        //url,分页导航
        return page;
    }

    @Override
    public Page<OrderItem> orderItemPage(int pageNo, int pageSize,String orderId) {
        Page<OrderItem> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        int totalRow=orderItemDao.getTotalRowByOrderId(orderId);
        page.setTotalRow(totalRow);
        int pageTotalCount=totalRow/pageSize;
        if(totalRow%pageSize>0){
            pageTotalCount++;
        }
        page.setPageTotalCount(pageTotalCount);
        //这里的begin需要计算
        //注意这里有一个坑
        int begin=(pageNo-1)*pageSize;
        List<OrderItem> orderItemByOrderId = getPageOrderItemsByOrderId(begin, pageSize, orderId);
        page.setItems(orderItemByOrderId);
        //url,分页导航
        return page;
    }

    @Override
    public BigDecimal getOrderItemTotalPrice(String orderId) {
        BigDecimal totalPrice=new BigDecimal(0);
        List<OrderItem> orderItemsByOrderId = getOrderItemsByOrderId(orderId);
        for (OrderItem orderItem : orderItemsByOrderId) {
            totalPrice=totalPrice.add(orderItem.getTotalPrice());
        }
        return totalPrice;
    }

    @Override
    public Integer getOrderItemTotalCount(String orderId) {
        int totalCount=0;
        List<OrderItem> orderItemsByOrderId = getOrderItemsByOrderId(orderId);
        for (OrderItem orderItem : orderItemsByOrderId) {
            totalCount+=orderItem.getCount();
        }
        return totalCount;
    }


    @Override
    public List<OrderItem> getOrderItemsByOrderId(String orderId) {
        return orderItemDao.getOrderItemsByOrderId(orderId);
    }

}
